from manim import *

class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-40, -5, 0],
            [-30,-5,0],
            [-30,5,0],
            [-40,5,0],
            [-40,-5,0]
        ]
        self.set_points_smoothly(points)


class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设}$D=\left\{  \left( x,y\right) | \left( x-1\right) ^{2}+\left( y-1\right) ^{2}\leq 2\right\} ,$\text{则}$\iint _{D}\left( x+y\right) d\sigma =$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0)
        
        

        self.play(Write(text),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,20,0]))


        


          # 创建坐标系
        axes = Axes(
            x_range=[-3, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-3, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes.move_to([-35, 0, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

        title = Tex(
                    r"$f\left( x\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,14,0])

       


        #self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        curve = CustomCurve(color=PURE_RED, stroke_width=20)
        
        self.play(Create(axes))
        self.play(  Create(curve))


        #下划线
        #下划线
        dot1=Dot([-35,10,0])
        dot2=Dot([-35,-10,0])
        line=Line(dot1,dot2).set_color(PURPLE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))

        text1 = Tex(r"\text{关于}$x=1$\text{对称}",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-15,0])
        text2 = Tex(r"\text{(奇偶对称性)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text1,RIGHT,buff=1)
        

        self.play(Create(text1))
        self.play(Create(text2))
        dot1=Dot([-45,0,0])
        dot2=Dot([-25,0,0])
        line=Line(dot1,dot2).set_color(PURE_BLUE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))

        text1 = Tex(r"\text{关于}$y=1$\text{对称}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-20,0])
        text2 = Tex(r"\text{(奇偶对称性)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text1,RIGHT,buff=1)
        

        self.play(Create(text1))
        self.play(Create(text2))

        dot1=Dot([-45,-10,0])
        dot2=Dot([-25,10,0])
        line=Line(dot1,dot2).set_color(PURE_GREEN)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))

        text1 = Tex(r"\text{关于}$y=x$\text{对称}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-25,0])
        self.play(Create(text1))
        text2 = Tex(r"\text{(轮换对称性)}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text1,RIGHT,buff=1)

        

        self.play(Create(text2))

         #箭头引出
        arrow = Arrow(start=[15,25,0],end=[20,23,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征识别:\n"
                     "一次形式的被积函数")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([0,25,0])
        #框框标
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=7.5,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([23.5, 20, 0])
        self.play(Create(rectangle),Create(text100))



        text1 = Tex(r"$\iint _{D}\left[ \left( x-1\right) +\left( y-1\right) +2\right] d\sigma $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22.5,12,0])
        self.play(Create(text1))


        dot1=Dot([11,10,0])
        dot2=Dot([30,10,0])
        line=Line(dot1,dot2).set_color(YELLOW)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))

        text1 = Tex(r"$0$",color=PURE_GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([21,7,0])
        self.play(Create(text1))

        text1 = Tex(r"$=\iint _{D}2d\sigma $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22.5,0,0])
        self.play(Create(text1))

        text2 = Tex(r"$=2S_{D}$",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17.5,0,0])

        
        self.play(
            ReplacementTransform(text1,text2)
        )
        self.wait(0.2)

        text3 = Tex(r"$=4\pi $",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=1)

        self.play(Create(text3))
        
        dot1=Dot([-5,-5,0])
        dot2=Dot([48,-5,0])
        line=Line(dot1,dot2).set_color(BLUE)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))

         #箭头引出
        arrow = Arrow(start=[-20,7,0],end=[-25,5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))
        #文本
        long_text=("特征识别:\n"
                     "区域为圆的一部分")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([-8,10,0])
        self.play(Create(text100))

        text3 = Tex(r"\text{(极坐标)}",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text100,DOWN,buff=1)

        self.play(Create(text3))

        text4 = Tex(r"\text{(极坐标平移)}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text100,DOWN,buff=1)

        self.play(
            ReplacementTransform(text3,text4)
        )
        self.wait(0.2)

        text1 = Tex(r"$\begin{cases}x-1=r\cos \theta \\y-1=r\sin \theta \end{cases}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,0,0])
        self.play(Create(text1))

        text1 = Tex(r"$\int _{0}^{2\pi }d\theta \int _{0}^{\sqrt{2}}\left( r\left( \sin \theta +\cos \theta \right) +2\right) rdr$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-10,0])
        
        text10 = Tex(r"$\int _{0}^{2\pi }d\theta \int _{0}^{\sqrt{2}}\left( r\left( \sin \theta +\cos \theta \right) +2\right) rdr$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-10,0])
        text100 = Tex(r"$\int _{0}^{2\pi }d\theta \int _{0}^{\sqrt{2}}\left( r\left( \sin \theta +\cos \theta \right) +2\right) rdr$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,-10,0])
        self.play(Create(text1),Create(text10),Create(text100))

        text11 = Tex(r"$\int _{0}^{2\pi }d\theta \int _{0}^{\sqrt{2}}r^{2}\left( \sin \theta +\cos \theta \right) dr$",color=YELLOW_C
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-15,0])
        

        text21 = Tex(r"$+\int _{0}^{2\pi }d\theta \int _{0}^{\sqrt{2}}2rdr$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text11,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text10,text11),
            ReplacementTransform(text100,text21)
        )
        self.wait(0.2)

        text1 = Tex(r"$2\pi \times 2$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([37,-20,0])
        self.play(Create(text1))

        text10 = Tex(r"$\int _{0}^{2\pi }\left( \sin \theta +\cos \theta \right) d\theta \int _{0}^{\sqrt{2}}r^{2}dr$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text11,DOWN,buff=1)
        self.play(Create(text10))

        text100 = Tex(r"$0\times \dfrac{2}{3}\sqrt{2}$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text10,DOWN,buff=1)
        self.play(Create(text100))
        self.wait()